package com.home766.controller.shop;

import com.home766.dto.DictionaryDTO;
import com.home766.entity.Scheme;
import com.home766.entity.SchemeProduct;
import com.home766.service.IDictionaryService;
import com.home766.service.SchemeProductService;
import com.home766.service.SchemeService;
import com.home766.util.StringUtil;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.controller.shop.BaseController;
import net.biz.service.BrandService;
import net.biz.util.ParamsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 前台方案-controller
 */
@Controller
@RequestMapping("/shop/scheme")
public class ShopSchemeController extends BaseController {

    @Resource(name = "schemeServiceImpl")
    private SchemeService schemeService;
    @Resource(name = "schemeProductServiceImpl")
    private SchemeProductService schemeProductService;
    @Autowired
    private IDictionaryService iDictionaryService;
    @Resource(name = "brandServiceImpl")
    private BrandService brandService;

    /**
     * 方案列表
     *
     * @param pageable
     * @param model
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model, Integer acreage, Integer attribute, Long brandId, Long auxiliaryId, Long type) {
        Page<Scheme> page = schemeService.findPage(pageable, acreage, attribute, brandId, auxiliaryId, null, type);
        model.addAttribute("page", page);
        if (type == 14) {
            model.addAttribute("brands", schemeProductService.getSchemeProductDtoList());
            model.addAttribute("brandsAuxiliary", schemeProductService.getSchemeProductDtoListOfAuxiliary());
        } else if (type == 105) {
            model.addAttribute("brands", schemeProductService.getSchemeProductDtoListByConditioner());
        }
        //查询对应类型的属性值
        List<DictionaryDTO> schemeAttrs = iDictionaryService.searchChildren(ParamsUtil.SCHEME_ATTRIBUTE);
        //查询面积范围属性标签值
        List<DictionaryDTO> areaTags = iDictionaryService.searchChildren(ParamsUtil.SCHEME_AREA_TAG);
        model.addAttribute("areaTags", areaTags);
        model.addAttribute("schemeAttrs", schemeAttrs);
        model.addAttribute("attribute", attribute);
        //智能暖气主材
        model.addAttribute("brandId", brandId);
        //智能暖气辅材
        model.addAttribute("auxiliaryId", auxiliaryId);
        model.addAttribute("acreage", acreage);
        model.addAttribute("type", type);
        return "/shop/scheme/list";
    }

    /**
     * 方案详情
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/content", method = RequestMethod.GET)
    public String content(ModelMap model, Long id) {
        if (id == null) {
            return ERROR_VIEW;
        }
        Scheme scheme = schemeService.find(id);
        if (scheme == null) {
            return ERROR_VIEW;
        }

        List<SchemeProduct> schemeProducts = scheme.getSchemeProducts();
        if (schemeProducts != null) {
            Collections.sort(schemeProducts, new Comparator<SchemeProduct>() {
                @Override
                public int compare(SchemeProduct o1, SchemeProduct o2) {
                    if (o1.getSortWeight() == null && o2.getSortWeight()!=null) {
                        return 1;
                    }else if(o1.getSortWeight() != null && o2.getSortWeight()==null){
                        return -1;
                    }
                    if (o1.getSortWeight()==null && o2.getSortWeight() == null) {
                        return 1;
                    }
                    return o2.getSortWeight() - o1.getSortWeight();
                }
            });
        }
        model.addAttribute("scheme", scheme);
        List<DictionaryDTO> types = iDictionaryService.searchChildren(ParamsUtil.SCHEME_TYPE);
        model.addAttribute("types", types);
        List<Long> attrs = new ArrayList<Long>();
        if (!StringUtil.isNullOrSpace(scheme.getAttribute())) {
            String[] sattr = scheme.getAttribute().split(",");
            for (String s : sattr) {
                attrs.add(Long.parseLong(s));
            }
            model.addAttribute("attrs", attrs);
        }
        //查询对应类型的属性值
        List<DictionaryDTO> schemeAttrs = iDictionaryService.searchChildren(ParamsUtil.SCHEME_ATTRIBUTE);
        model.addAttribute("schAttrs", schemeAttrs);
        model.addAttribute("typeIdZNKT", ParamsUtil.SCHEME_PRODUCT_ID_ZNKT);
        model.addAttribute("typeIdZNNQ", ParamsUtil.SCHEME_PRODUCT_ID_ZNNQ);
        model.addAttribute("categoryIdDL", ParamsUtil.SCHEME_PRODUCT_ID_DL);
        model.addAttribute("mingzhuangId", ParamsUtil.SCHEME_PRODUCT_ID_ZNMZ);
        model.addAttribute("anzhuangId", ParamsUtil.SCHEME_PRODUCT_ID_ZNAZ);
        return "/shop/scheme/content";
    }

    /**
     * 自定义方案
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/diycontent", method = RequestMethod.GET)
    public String diycontent(ModelMap model, Long id) {
        if (id == null) {
            return ERROR_VIEW;
        }
        Scheme scheme = schemeService.find(id);
        if (scheme == null) {
            return ERROR_VIEW;
        }


        model.addAttribute("scheme", scheme);
        List<DictionaryDTO> types = iDictionaryService.searchChildren(ParamsUtil.SCHEME_TYPE);
        model.addAttribute("types", types);
        List<Long> attrs = new ArrayList<Long>();
        if (!StringUtil.isNullOrSpace(scheme.getAttribute())) {
            String[] sattr = scheme.getAttribute().split(",");
            for (String s : sattr) {
                attrs.add(Long.parseLong(s));
            }
            model.addAttribute("attrs", attrs);
        }
        //查询对应类型的属性值
        List<DictionaryDTO> schemeAttrs = iDictionaryService.searchChildren(ParamsUtil.SCHEME_ATTRIBUTE);
        model.addAttribute("schAttrs", schemeAttrs);
        model.addAttribute("typeIdZNKT", ParamsUtil.SCHEME_PRODUCT_ID_ZNKT);
        model.addAttribute("typeIdZNNQ", ParamsUtil.SCHEME_PRODUCT_ID_ZNNQ);
        model.addAttribute("categoryIdDL", ParamsUtil.SCHEME_PRODUCT_ID_DL);
        model.addAttribute("mingzhuangId", ParamsUtil.SCHEME_PRODUCT_ID_ZNMZ);
        model.addAttribute("anzhuangId", ParamsUtil.SCHEME_PRODUCT_ID_ZNAZ);
        return "/shop/scheme/diyscheme";
    }

    /**
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/findByCategoryId", method = RequestMethod.POST)
    @ResponseBody
    public List<SchemeProduct> content(Long categoryId) {
        if (categoryId == null) {
            return null;
        }
        //查询该方案类型对应的所有主材产品
        List<SchemeProduct> schemeProducts = schemeProductService.findByCategory(categoryId, true);
        return schemeProducts;
    }

}
